package control;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.AbstractAction;
import javax.swing.KeyStroke;

import org.netbeans.validation.api.Problem;
import org.netbeans.validation.api.Problems;
import org.netbeans.validation.api.Severity;
import org.netbeans.validation.api.ui.ValidationGroup;
import org.netbeans.validation.api.ui.ValidationListener;
import org.netbeans.validation.api.ui.ValidationUI;

import component.Icons;

import view.BookDetailJPanel;
import view.NotCloseableException;

@SuppressWarnings("serial")
public class SaveBookAction extends AbstractAction implements ValidationUI {
	BookDetailJPanel panel;

	public SaveBookAction(BookDetailJPanel panel) {
		super("Buch speichern und schliessen", Icons.SAVE);
		this.panel = panel;
		putValue(SHORT_DESCRIPTION, "Buch Speichern und schliessen");
		putValue(MNEMONIC_KEY, KeyEvent.VK_S);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		panel.saveModel();
		try {
			panel.tryToClose();
		} catch (NotCloseableException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	@Override
	public void clearProblem() {
		setEnabled(true);
	}

	@Override
	public void setProblem(Problem p) {
		if(p.isFatal()){
			setEnabled(false);
		}
	}
}
